﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using QunYing.BLL;

namespace Client
{
    public partial class FrmActivityGoods : BaseChildForm
    {
        public static FrmActivityGoods frm = null;
        public FrmActivityGoods()
        {
            InitializeComponent();
            this.sharpGrid1.AddPopupMenu(new ToolStripSeparator(), null);
            this.sharpGrid1.AddPopupMenu(new ToolStripMenuItem("删除选择活动"), RemoveActivity);
            frm = this;
        }

        private void FrmActivityGoods_Load(object sender, EventArgs e)
        {
            QueryData();
        }

        public void QueryData()
        {
            QunYing.BLL.ActivityGoods goodsInfo = new ActivityGoods();
            DataTable dt = goodsInfo.GetList(GetFiter()).Tables[0];
            sharpGrid1.DataSource = dt;

            GridView view = this.sharpGrid1.MainView as GridView;
            view.Columns["活动ID"].Visible = false;
            view.Columns["经销商编号"].Visible = false;
            view.Columns["宣传图片"].Visible = false;
            view.Columns["显示顺序"].Visible = false;
        }
        private string GetFiter()
        {
            StringBuilder strFiter = new StringBuilder();
            strFiter.Append(" 经销商编号='" + GlobalInfo.UserLoginInfo.用户ID + "'");
            if (spanDate.StartDateEdit.Text!= "")
            {
                strFiter.Append(" AND 开始时间 >= '" + spanDate.StartDateEdit.DateTime.ToString("yyyy-MM-dd") + "'");
            }
            else if (spanDate.EndDateEdit.Text != "")
            {
                strFiter.Append(" AND 开始时间 <= '" + spanDate.StartDateEdit.DateTime.ToString("yyyy-MM-dd") + " 23:59:59'");
            }
            strFiter.Append(" Order by 活动ID DESC");
            return strFiter.ToString();
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            QueryData();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            FrmAddActivity frm = new FrmAddActivity();
            frm.ShowDialog();
        }

        private void RemoveActivity(object sender, EventArgs e)
        {
            GridView view = this.sharpGrid1.MainView as GridView;
            int[] selectedRows = view.GetSelectedRows();
            if (selectedRows.Length == 0)
            {
                GlobalInfo.MsgBoxShowError("请选择你要删除的活动");
                return;
            }
            else
            {
                if (XtraMessageBox.Show(GlobalInfo.UserLookAndFeel, "确认删除该活动吗？", "询问", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.No)
                    return;
                foreach (var v in selectedRows)
                {
                    object obj = view.GetRow(v);
                    DataRowView row = obj as DataRowView;
                    if (row == null)
                        continue;
                    QunYing.BLL.ActivityGoods activityGoods = new ActivityGoods();
                    activityGoods.Delete(Convert.ToInt32(row.Row["活动ID"]));

                    try
                    {
                        QunYing.Common.IImageOperator opera = QunYing.Common.ClientProxyFactory.GetProxy<QunYing.Common.IImageOperator>(QunYing.Common.GlobalInfo.ServiceImageOperationVip);
                        opera.DeleteImg(new string[] { row.Row["宣传图片"].ToString() });
                        QunYing.Common.ClientProxyFactory.Close(opera);
                    }
                    catch (Exception ex)
                    {
                        QunYing.Common.Loger.Log.Error("删除图片发生错误:" + ex.Message);
                    }
                }

                QueryData();
            }
        }

        private void sharpGrid1_RowDoubleClickEvent(object sender, Spore.Controls.RowDoubleClickEventArg e)
        {
            GridView view = this.sharpGrid1.MainView as GridView;
            int[] selectedRows = view.GetSelectedRows();
            foreach (var v in selectedRows)
            {
                if (v >= 0)
                {
                    object obj = view.GetRow(v);
                    DataRowView row = obj as DataRowView;
                    if (row == null)
                        continue;
                    QunYing.Model.ActivityGoods model = new QunYing.BLL.ActivityGoods().GetModel(Convert.ToInt32(row.Row["活动ID"]));
                    FrmAddActivity frm = SingleObjManager.GetSingleObj<FrmAddActivity>();
                    frm.OPERTYPE = "EDIT";
                    frm.Text = "修改活动";
                    frm.editModel = model;
                    frm.ShowDialog();
                    break;
                }
            }
        }
        public override void BindData()
        {
            QueryData();
        }
    }
}